package com.note.chh.notebook.UI.enjoy;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.UI.enjoy.netMusicFragment.NewMusicFragment;
import com.note.chh.notebook.UI.enjoy.netMusicFragment.RankFragment;
import com.note.chh.notebook.UI.enjoy.netMusicFragment.SingerFragment;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

//网络音乐
public class NetMusicActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mRadio_type)
    RadioGroup mRadioType;
    @BindView(R.id.mViewPager_type)
    ViewPager mViewPagerType;
    @BindView(R.id.mRadio_rank)
    RadioButton mRadioRank;
    @BindView(R.id.mRadio_new)
    RadioButton mRadioNew;
    @BindView(R.id.mRadio_singer)
    RadioButton mRadioSinger;

    @Override
    protected int bindLayout() {
        return R.layout.activity_net_music;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.net_music));
    }

    @Override
    public void initView() {

        QMUIStatusBar(NetMusicActivity.this);

        MusicTypeAdapter musicTypeAdapter = new MusicTypeAdapter(getSupportFragmentManager());
        mViewPagerType.setAdapter(musicTypeAdapter);
        mViewPagerType.addOnPageChangeListener(musicTypeAdapter);

        mRadioType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mRadioRank.getId()==checkedId) {
                    if (mRadioRank.isChecked()) {
                        mViewPagerType.setCurrentItem(0);
                    }
                } else if (mRadioNew.getId()==checkedId) {
                    if (mRadioNew.isChecked()) {
                        mViewPagerType.setCurrentItem(1);
                    }
                }else{
                    if (mRadioSinger.isChecked()) {
                        mViewPagerType.setCurrentItem(2);
                    }
                }
            }
        });
    }

    @Override
    public void initData() {

    }



    private class MusicTypeAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
        private List<Fragment> fragments = new ArrayList<>();

        public MusicTypeAdapter(FragmentManager fm) {
            super(fm);
            fragments.add(new RankFragment());
            fragments.add(new NewMusicFragment());
            fragments.add(new SingerFragment());
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                if (!mRadioRank.isChecked()) {
                    mRadioRank.setChecked(true);
                }
            } else if (position == 1) {
                if (!mRadioNew.isChecked()) {
                    mRadioNew.setChecked(true);
                }
            } else if(position == 2){
                if (!mRadioSinger.isChecked()) {
                    mRadioSinger.setChecked(true);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
